package com.tushen.task;

import com.tushen.utils.SqlSessionFactoryUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 表层浓度场处理线程
 * @author likun
 * @version 1.0
 * @date 2020/12/13 3:08 下午
 */
public class DatThread extends Thread {
    String fileDir;
    String taskid;
    String version;
    String outDir="D:\\geoserver-2.17.1\\data_dir\\data\\polcon2";

    public DatThread(String fileDir, String taskid, String version){
        this.fileDir=fileDir;
        this.taskid=taskid;
        this.version=version;
    }

    public void run(){

        int status=0;
        File file=new File(fileDir);
        String dtmMin="";
        String dtmMax="";
        if(file.isDirectory()){
            File[] files=file.listFiles();
            for (int i=0;i<files.length;i++){
                if(files[i].getName().endsWith("dat")) {
                    status=2;
             }
            }
        }
        if(status==2){
            String[] dtms=getMinMaxDtm(fileDir);
            dtmMin=dtms[0];
            dtmMax=dtms[1];
            Dat.createTifs(fileDir,outDir);
            String updateVersionSql=
                    "insert into data_dtm_verion(m_type,m_version,start_dtm,end_dtm) " +
                            "values('dye',"+version+", to_timestamp('"+dtmMin+"','yyyyMMddHH24mi'),to_timestamp('"+dtmMax+"','yyyyMMddHH24mi')) " +
                            "  on  CONFLICT(m_type,m_version) do nothing;";
            SqlSessionFactoryUtil.excuteUpdateSql(updateVersionSql);
        }

        String updateTaskSql="update data_process_task set end_dtm=now(),status="+status+" where taskid='"+taskid+"'";
        SqlSessionFactoryUtil.excuteUpdateSql(updateTaskSql);

        System.out.println("快速表层浓度场处理完毕!");

    }

    //获取文件夹下的最大最小时间
    private String[] getMinMaxDtm(String fileDir){
        File file=new File(fileDir);
        String dtmMin="";
        String dtmMax="";
        Date dtmMi=null;
        Date dtmMa=null;
        if(file.isDirectory()){
            File[] files=file.listFiles();
            for (int i=0;i<files.length;i++){
                if(files[i].getName().endsWith("dat")) {
                    String regEx="[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(files[i].getName());
                    String dtmStr=m.replaceAll("").trim();
                    SimpleDateFormat smt=new SimpleDateFormat("yyyyMMddHHmm");
                    try {
                        Date dtm= smt.parse(dtmStr);
                        if(dtmMi==null||dtmMi.getTime()>dtm.getTime()){
                            dtmMi=dtm;
                            dtmMin=dtmStr;
                        }
                        if(dtmMa==null||dtmMa.getTime()<dtm.getTime()){
                            dtmMa=dtm;
                            dtmMax=dtmStr;
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        }
        return new String[]{dtmMin,dtmMax};
    }



}
